package com.mytestapp;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withResourceName;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;

import android.app.Activity;
import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ExampleInstrumentedTest {
    private static final String LAUNCHACTIVITY = "com.aipower.app.ui.common.StartActivity";
    private static Class<? extends Activity> launchActivityClass;


    static {
        try {
            launchActivityClass = (Class<? extends Activity>) Class.forName(LAUNCHACTIVITY);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    @Rule
    public ActivityTestRule<?> rule= new ActivityTestRule<>(launchActivityClass); // androidTestImplementation 'androidx.test:rules:1.2.0' 版本才能正常用，且能启动app
//    public ActivityScenarioRule<? extends Activity> rules= new ActivityScenarioRule<>(launchActivityClass); # 无源码的测试无法启动app


    @Test
    public void useAppContext() throws InterruptedException {

        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.aipower.app", appContext.getPackageName());
        onView(withResourceName("cbPrivacy")).check(matches(not(doesNotExist())));
//        onView(withResourceName("cbPrivacy")).perform(click());
//        onView(withResourceName("cbPrivacy")).check(doesNotExist());
////        onView(withClassName()).perform(click());
//        onView(withResourceName("tv_jump")).perform(click());

        Thread.sleep(15000);

    }
}